#!/bin/bash
# get where I am
MY_PATH=$(readlink -f  $(dirname $0))

# what type of terminal
OPTION_WORKING_DIR='--working-directory'
OPTION_CORE_GEOMETRY='--geometry=50x10+0+0'
OPTION_RM_GEOMETRY='--geometry=50x10+500+0'
OPTION_TITLE='--title'
OPTION_COMMAND='--command'

if [ $(which mate-terminal) ]; then
    TERMINAL=mate-terminal
elif [ $(which xfce4-terminal) ]; then
    TERMINAL=xfce4-terminal
elif [ $(which lxterminal) ]; then
    TERMINAL=lxterminal
elif [ $(which konsole) ]; then
    TERMINAL=konsole
    OPTION_WORKING_DIR='--workdir'
    OPTION_CORE_GEOMETRY=''
    OPTION_RM_GEOMETRY=''
    OPTION_PM_GEOMETRY=''
    OPTION_COMMAND='-e'
    OPTION_TITLE='-T'
fi

MASTER_DISPLAY_OPTION="${OPTION_CORE_GEOMETRY} ${OPTION_TITLE}=\"roscore\""
RUNMGR_DISPLAY_OPTION="${OPTION_RM_GEOMETRY} ${OPTION_TITLE}=\"runtime_manager\""

if [ $(which gnome-terminal) ]; then
    TERMINAL=gnome-terminal
    GNOME_VERSION=$(gnome-terminal --version | cut -d '.' -f 2)
    if [ ${GNOME_VERSION} -ge 14 ]; then
	MASTER_DISPLAY_OPTION=''
	RUNMGR_DISPLAY_OPTION=''
    fi
fi

echo "Process Manager"

sudo bash -c "$MY_PATH/run_proc_manager &"

ADDR=$($MY_PATH/subnet_chk.py -)
if [ "$?" == "0" ]; then
  export ROS_IP=$ADDR
fi

# boot ros-master & health_checker
#${TERMINAL} ${MASTER_DISPLAY_OPTION} ${OPTION_WORKING_DIR}=${MY_PATH} ${OPTION_COMMAND}="bash -c 'source ./../../../install/setup.bash; mkdir ~/.rviz; cp ./../documentation/examples/launch/rosbag_demo/default.rviz ~/.rviz/; roscore'"&

# boot runtime_manager
${TERMINAL} ${RUNMGR_DISPLAY_OPTION} ${OPTION_WORKING_DIR}=${MY_PATH} ${OPTION_COMMAND}="bash -c 'source ./../../../setup.bash; mkdir ~/.rviz; cp `rospack find autoware_quickstart_examples`/launch/rosbag_demo/default.rviz ~/.rviz/; rosrun runtime_manager runtime_manager_dialog.py'"
